package com.deity.news.interceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.deity.news.util.Constant;
import com.deity.news.util.CookieManager;
import com.deity.news.util.UserRoleUtil;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;

public class IsAdminInterceptor implements Interceptor {

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void init() {
		// TODO Auto-generated method stub
		
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		if(invocation.getAction() instanceof IsAdminAware){
			HttpServletRequest request=ServletActionContext.getRequest();
			Cookie userCookie = CookieManager.getCookie(request, Constant.COOKIE_NAME);
			String username;
			if(userCookie!=null){
				String cookieValue = userCookie.getValue();
				username = cookieValue.split("/")[0];
			}else{
				username="";
			}
			
			((IsAdminAware)invocation.getAction()).setAdmin(UserRoleUtil.isAdmin(username));
		}
		return invocation.invoke();
	}

}
